package com.jt.sp03.user.service;

import com.fasterxml.jackson.core.type.TypeReference;
import com.jt.sp01.pojo.User;
import com.jt.sp01.service.UserService;
import com.jt.web.util.JsonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
@RefreshScope//刷新配置时,重新加载类
public class UserServiceImpl implements UserService {
    @Value("${sp.user-service.users}")
    private String userJson;

    @Override
    public User getUser(Integer id) {
        //利用继承语法来写出List<User>
        List<User> users = JsonUtil.from(userJson, new TypeReference<List<User>>() {
        });
        for (User user : users) {
            if (user.getId().equals(id)) {
                return user;
            }
        }

        return new User(id, "用户名" + id, "密码" + id);
    }

    @Override
    public void addScore(Integer id, Integer score) {
        // 这里增加积分
        log.info("user " + id + " - 增加积分 " + score);

    }
}
